package com.aplink.generic.widgets;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.adapter.GenericConfigAdapter;
import com.aplink.generic.constant.BundleConstant;
import com.aplink.generic.constant.ConfigParameter;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropCol;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.ListviewSpec;
import com.aplink.generic.pojo.xml.ColumnSetting;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.util.convertcount.GenericFilter;
import com.aplink.generic.util.objectrule.TargetRuleCollection;
import com.aplink.generic.xmlparser.ColumnSettingParser;
import com.aplink.generic.xmlparser.ObjPropInfoParser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CustomListView extends BaseView {

    private ArrayList<ColumnSetting> columnSettings;

    private final ListView listView;

    private ArrayList<GenericObject> mGenericObjects;
    private LogUtil mLogUtil = new LogUtil( getClass() );
    private GenericConfigAdapter mGenericConfigAdapter;
    private TargetRuleCollection targetCollection;
    private ListviewSpec mListviewSpecFormat;

    public GenericConfigAdapter getGenericConfigAdapter() {
        return mGenericConfigAdapter;
    }

    public void setGenericConfigAdapter(
            GenericConfigAdapter genericConfigAdapter ) {
        mGenericConfigAdapter = genericConfigAdapter;
    }

    public CustomListView( GenericFragment dataEntryActivity,
                           ControlSpec controlSpec, View view ) {
        super( dataEntryActivity, controlSpec, view );
        this.listView = (ListView) view;
        mListviewSpecFormat = (ListviewSpec) getControlSpec();
        targetCollection = mListviewSpecFormat.getTargetRuleCollection();
    }

    private GenericConfigAdapter generateConfigAdapter() {
        this.columnSettings = ColumnSettingParser.parserIntoColumnSetting(
                getApplicationContext(), mListviewSpecFormat.getIdRptrDesign(),
                ConfigParameter.COL_TAGNAME_COLUMN );
        return new GenericConfigAdapter( getApplicationContext(),
                this.mGenericObjects, this.columnSettings,
                mListviewSpecFormat.getRowItem() );
    }

    @Override
    protected void loadDataToView() {

    }

    @Override
    protected void doIfNotHashPassedValues() {

    }

    protected ArrayList<GenericObject> loadData() {
        ObjPropCol objPropCol = ObjPropInfoParser.parseIntoObjProp(
                getApplicationContext(), getContentDataFileId(),
                ConfigParameter.OBJ_PROP_TAGNAME,
                mListviewSpecFormat.getDataObject() );

        // if (mListviewSpecFormat.hasSelectWhere()) {
        // final String selectWhere = mListviewSpecFormat.getSelectWhere();
        // final String[] selectWheres = new String[] { selectWhere };
        // final String[] values = new String[] { getHashPassedValues().get(
        // selectWhere) };
        // return getSelectCommand().getObjectsEmbeddedParents(objPropList,
        // selectWheres, values);
        // }
        return getSelectCommand().getList( objPropCol, null );
    }

    @Override
    protected void setEventListener() {
        this.listView.setOnItemClickListener( mOnRowClickListener );
    }

    private OnItemClickListener mOnRowClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick( AdapterView<?> arg0, View arg1, int index,
                                 long arg3 ) {
            if ( mListviewSpecFormat.getTargetRuleCollection() != null ) {
                String fullClassName = null;
                try {
                    GenericObject genericObject = mGenericConfigAdapter
                            .getDisplayResult().get( index );
                    fullClassName = GenericFilter.getTargetActivityFromRule(
                            targetCollection, genericObject );
                    if ( fullClassName == null ) {
                        fullClassName = targetCollection.getDefaultTarget();
                    }
                    Class<?> targetActivity = Class.forName( fullClassName );
                    Intent intent = new Intent( getApplicationContext(),
                            targetActivity );

                    Bundle bundle = new Bundle();
                    bundle.putSerializable( BundleConstant.LIST_OBJECT,
                            genericObject );
                    bundle.putInt( BundleConstant.SELECTED_INDEX, index );
                    intent.putExtras( bundle );
                    getActivity().startActivity( intent );
                } catch ( ClassNotFoundException e ) {
                    e.printStackTrace();
                    mLogUtil.error( fullClassName + " is not found",
                            "List view item click", e );
                }
            }
        }
    };

    @Override
    protected void initViewProperties() {
        this.mGenericObjects = this.loadData();
        if ( mGenericObjects != null ) {
            this.mGenericConfigAdapter = this.generateConfigAdapter();
            this.listView.setAdapter( this.mGenericConfigAdapter );
        }
    }

    @Override
    public GenericAttribute<?, ?> getAttribute() {
        return null;
    }

    public void filter( String s ) {
        mGenericConfigAdapter.getFilter().filter( s );
        mGenericConfigAdapter.notifyDataSetChanged();
    }

    public void sort( Comparator<GenericObject> comparator ) {
        if ( mGenericObjects != null ) {
            ArrayList<GenericObject> resultGenericObjects = mGenericObjects;
            Collections.sort( resultGenericObjects, comparator );
            setAdapter( resultGenericObjects );
        }
    }

    private void setAdapter( ArrayList<GenericObject> genericObjects ) {
        mGenericConfigAdapter.setDisplayResult( genericObjects );
        mGenericConfigAdapter.notifyDataSetChanged();
    }
}
